package Boke;

public class protectThread {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main主线程，begin");
        Thread t1=new Thread(()->{
            System.out.println("前台线程，begin");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("前台线程，end");
        });
        t1.start();
        Thread t2=new Thread(()->{
            //保证后台线程的run执行时间大于前台线程t1
            System.out.println("后台线程，begin");
            try {
                //让后台线程休眠较长时间
                //保证主线程和前台线程t1都可快速执行完
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("后台线程，end");
        });
        //设置后台线程要在start方法前
        t2.setDaemon(true);
        t2.start();

        System.out.println(t2.isDaemon());
        System.out.println("main主线程，end");
    }
}
